package Forms;

import Classes.*;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Owner
 */
public class OpenSY extends javax.swing.JFrame {
    static MInterface mi;
    String port;
    _Login l = new _Login();
    
    static User cUser;
    String option;
    SchoolYear sy;    
    ArrayList<Object> syList, currList;

    /**
     * Creates new form CurriculumSchoolYear
     */
    public OpenSY(User user, MInterface i) {
        initComponents();
        mi = i;
        
        try {    
            int previous;
            int start = mi.getToday();
            previous = Integer.valueOf(start);            
            
            int next;
            String end = mi.getRealYear();
            next = Integer.valueOf(end);               
                       
            for(int x = previous; x < previous + 100; x++) {
                for(int y = x + 1; y < previous + 100; y++) {                   
                    syCb.addItem(x + "-" + y);                   
                    x = y;
                }
            }   
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void compute() {
        int schoolDays = 0;
        Calendar st = Calendar.getInstance();
        Calendar en = Calendar.getInstance();
                                        
        Date s = startChooser.getDate();
        Date e = endChooser.getDate();
                    
        st.setTime(s);
        en.setTime(e);
                    
        long milliseconds1 = st.getTimeInMillis();
        long milliseconds2 = en.getTimeInMillis();
        long diff = milliseconds2 - milliseconds1;
                    
        long ans = diff / (24 * 60 * 60 * 1000);
                    
        do {
            st.add(Calendar.DAY_OF_MONTH, 1);
                if (st.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && st.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY && en.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && en.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                    schoolDays++;
                } 
         } while (st.getTimeInMillis() < en.getTimeInMillis());
            numFld.setText(String.valueOf(schoolDays));
    }    
    

    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        syCb = new javax.swing.JComboBox();
        numFld = new javax.swing.JTextField();
        startChooser = new com.toedter.calendar.JDateChooser();
        endChooser = new com.toedter.calendar.JDateChooser();
        nxtBtn = new javax.swing.JButton();
        currCb = new javax.swing.JComboBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        nxtBtn1 = new javax.swing.JButton();
        nxtBtn2 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Open School Year");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/banner.jpg"))); // NOI18N
        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("School Year:");

        jSeparator1.setForeground(new java.awt.Color(0, 102, 51));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setText("Start Date:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setText("End Date:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel5.setText("Curriculum:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel7.setText("Number of School Days:");

        syCb.setFont(new java.awt.Font("Tahoma", 0, 12));

        numFld.setEditable(false);
        numFld.setFont(new java.awt.Font("Tahoma", 0, 12));

        startChooser.setDateFormatString("yy-MM-dd");
        startChooser.setFont(new java.awt.Font("Tahoma", 0, 12));

        endChooser.setDateFormatString("yy-MM-dd");
        endChooser.setFont(new java.awt.Font("Tahoma", 0, 12));

        nxtBtn.setFont(new java.awt.Font("Tahoma", 0, 14));
        nxtBtn.setText("Cancel");
        nxtBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nxtBtnActionPerformed(evt);
            }
        });

        currCb.setFont(new java.awt.Font("Tahoma", 0, 12));

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Curriculums for this School Year"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(jTable2);

        jButton1.setText("Add");

        nxtBtn1.setFont(new java.awt.Font("Tahoma", 0, 14));
        nxtBtn1.setText("Save");
        nxtBtn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nxtBtn1ActionPerformed(evt);
            }
        });

        nxtBtn2.setFont(new java.awt.Font("Tahoma", 0, 14));
        nxtBtn2.setText("Compute");
        nxtBtn2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nxtBtn2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(startChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(currCb, 0, 422, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 410, Short.MAX_VALUE)
                        .addComponent(nxtBtn1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nxtBtn))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(13, 13, 13)
                                .addComponent(endChooser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(syCb, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(numFld, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(nxtBtn2))
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 552, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSeparator1))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(syCb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(startChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(endChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(numFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nxtBtn2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(currCb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nxtBtn)
                    .addComponent(nxtBtn1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nxtBtn2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nxtBtn2ActionPerformed
        compute();
    }//GEN-LAST:event_nxtBtn2ActionPerformed

    private void nxtBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nxtBtnActionPerformed
        this.hide();
    }//GEN-LAST:event_nxtBtnActionPerformed

    private void nxtBtn1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nxtBtn1ActionPerformed
        try {
            Calendar st = Calendar.getInstance();
            Calendar en = Calendar.getInstance();

            Date s = startChooser.getDate();
            Date e = endChooser.getDate();
            
            st.setTime(s);
            en.setTime(e);
            
            String add;
            String startD = ((JTextField)startChooser.getDateEditor().getUiComponent()).getText();
            String endD = ((JTextField)endChooser.getDateEditor().getUiComponent()).getText(); 
            String sySelect = syCb.getSelectedItem().toString();
                        
            if (startD.isEmpty() || endD.isEmpty()) {
                JOptionPane.showMessageDialog(null, "Please fill out the needed fields", "Error", JOptionPane.ERROR_MESSAGE);
                jLabel3.setForeground(Color.red);
                jLabel4.setForeground(Color.red);
            } else if (startChooser.getDate().compareTo(endChooser.getDate()) > 0 || endChooser.getDate().compareTo(startChooser.getDate()) < 0) {
                JOptionPane.showMessageDialog(null, "Invalid Date", "Error", JOptionPane.ERROR_MESSAGE);
                jLabel3.setForeground(Color.red);
                jLabel4.setForeground(Color.red);
            } else if (numFld.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null, "Please click compute button", "Error", JOptionPane.ERROR_MESSAGE);
                jLabel7.setForeground(Color.red);
            } else if (st.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY && st.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY && en.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY && en.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
                JOptionPane.showMessageDialog(null, "Please pick a day between Monday to Friday", "Error", JOptionPane.ERROR_MESSAGE);
            } else {                
//                add = mi.addSY(new SchoolYear(sySelect, startD, endD, Integer.valueOf(numFld.getText())), cUser);                
                JOptionPane.showMessageDialog(null, "School Year " + sySelect + " has been added", "Message", JOptionPane.INFORMATION_MESSAGE);
                this.hide();                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_nxtBtn1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(OpenSY.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(OpenSY.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(OpenSY.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(OpenSY.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new OpenSY(cUser, mi).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox currCb;
    private com.toedter.calendar.JDateChooser endChooser;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField numFld;
    private javax.swing.JButton nxtBtn;
    private javax.swing.JButton nxtBtn1;
    private javax.swing.JButton nxtBtn2;
    private com.toedter.calendar.JDateChooser startChooser;
    private javax.swing.JComboBox syCb;
    // End of variables declaration//GEN-END:variables
}
